package cmd

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/gogf/gf/v2/os/gcmd"
	"learn-goframe/internal/controller"
	"learn-goframe/internal/controller/admin"
	"learn-goframe/internal/service"
)

var (
	Main = gcmd.Command{
		Name:  "main",
		Usage: "main",
		Brief: "start http server",
		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
			s := g.Server()

			s.Group("/", func(group *ghttp.RouterGroup) {
				group.Middleware(
					ghttp.MiddlewareHandlerResponse,
					ghttp.MiddlewareCORS,
				)

				// 需要权限验证
				group.Group("/v1", func(g *ghttp.RouterGroup) {
					g.Group("/admin", func(g *ghttp.RouterGroup) {
						g.Middleware(
							service.Auth().Ctx,
							service.Auth().CtxAdmin,
						)
						g.Bind(
							admin.Solution,
						)
					})

					g.Group("/solution", func(g *ghttp.RouterGroup) {
						g.Middleware(
							service.Auth().Ctx,
						)
						g.Bind(
							controller.Solution,
						)
					})

				})
			})

			s.SetClientMaxBodySize(500 * 1024 * 1024) // 500M
			s.Run()
			return nil
		},
	}
)
